/*
 * MorseProcess.java
 *
 * Created on May 15, 2008, 2:09 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package morseme;

/**
 *
 * @author tt
 */
import java.util.*;

public class MorseProcess {

private static final String MAP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.,:?\'-/()\"@=;_";
private static Vector morseT = new Vector(50);

static{

morseT.insertElementAt(".-", 0); //A
morseT.insertElementAt("-...", 1); //B
morseT.insertElementAt("-.-.", 2); //C
morseT.insertElementAt("-..", 3); //D
morseT.insertElementAt(".", 4); //E
morseT.insertElementAt("..-.", 5); //F
morseT.insertElementAt("--.", 6); //G
morseT.insertElementAt("....", 7); //H
morseT.insertElementAt("..", 8); //I
morseT.insertElementAt(".---", 9); //J
morseT.insertElementAt("-.-", 10); //K
morseT.insertElementAt(".-..", 11); //L
morseT.insertElementAt("--", 12); //M
morseT.insertElementAt("-.", 13); //N
morseT.insertElementAt("---", 14); //O
morseT.insertElementAt(".--.", 15); //P
morseT.insertElementAt("--.-", 16); //Q
morseT.insertElementAt(".-.", 17); //R
morseT.insertElementAt("...", 18); //S
morseT.insertElementAt("-", 19); //T
morseT.insertElementAt("..-", 20); //U
morseT.insertElementAt("...-", 21); //V
morseT.insertElementAt(".--", 22); //W
morseT.insertElementAt("-..-", 23); //X
morseT.insertElementAt("-.--", 24); //Y
morseT.insertElementAt("--..", 25); //Z
morseT.insertElementAt(".----", 26); //1
morseT.insertElementAt("..---", 27); //2
morseT.insertElementAt("...--", 28); //3
morseT.insertElementAt("....-", 29); //4
morseT.insertElementAt(".....", 30); //5
morseT.insertElementAt("-....", 31); //6
morseT.insertElementAt("--...", 32); //7
morseT.insertElementAt("---..", 33); //8
morseT.insertElementAt("----.", 34); //9
morseT.insertElementAt("-----", 35); //0
morseT.insertElementAt(".-.-.-", 36); //.
morseT.insertElementAt("--..--", 37); //,
morseT.insertElementAt("---...", 38); //:
morseT.insertElementAt("..--..", 39); //?
morseT.insertElementAt(".----.", 40); //\'
morseT.insertElementAt("-....-", 41); //-
morseT.insertElementAt("-..-.", 42); ///
morseT.insertElementAt("-.--.-", 43); //(
morseT.insertElementAt("-.--.-", 44); //)
morseT.insertElementAt(".-..-.", 45); //"
morseT.insertElementAt(".--.-.", 46); //@
morseT.insertElementAt("-...-", 47); //=
morseT.insertElementAt("-.-.-", 48); //;
morseT.insertElementAt("..--.-", 49); //_
}

public static String translate(String org) {

String translated = "";
translated = org.trim().toUpperCase();

translated = eTrim(translated);

if (translated.length() <= 0) {
return "";
}

translated = translated.trim();
translated = translated.replace(' ', '|');

// while (isBlanks(translated)) {
//
// int first = translated.indexOf("||");
// int last = findLastIndex(translated, (first + 1), '|');
// translated = (translated.substring(0, first + 1)).concat(translated.
// substring(last + 1, translated.length()));
//
// }

translated = ignore(translated);

// while (isBlanks(translated)) {
//
// int first = translated.indexOf("||");
// int last = findLastIndex(translated, (first + 1), '|');
// translated = (translated.substring(0, first + 1)).concat(translated.
// substring(last + 1, translated.length()));
//
// }
translated = eTrim(translated);
translated = toMorse(translated);

return translated;
}

private static String eTrim(String translated){

if(translated.startsWith("|")){
translated = translated.substring(1, translated.length());
}

if(translated.endsWith("|")){
translated = translated.substring(0, translated.length()-1);
}

return translated;

}

private static String toMorse(String org) {

StringBuffer morse = new StringBuffer();

for (int i = 0; i < org.length(); i++) {

char c = org.charAt(i);
int idx = MAP.indexOf(c);

if (idx == -1 && c != '|')
continue;

if (c == '|' && idx == -1) {
morse = morse.append(c + " ");
}
else if (idx != -1) {
morse = morse.append(morseT.elementAt(idx).toString() + " ");
}
}
return morse.toString().trim();
}

private static String convert(String org) {

for (int i = 0; i < org.length(); i++) {

char c = org.charAt(i);
if (c == '*' || c == '|') {
//if(c == '*'){

continue;

}
else if (MAP.indexOf(c) == -1) {

org = org.replace(c, '*');
}
}

return org;
}

private static String ignore(String org) {

String tmp = org;
tmp = convert(tmp);

int idx = tmp.indexOf('*');
int last = -1;

while (idx != -1) {

last = findLastIndex(tmp, idx + 1, '*');
tmp = (tmp.substring(0, idx)).concat(tmp.substring(last + 1, tmp.length()));

idx = tmp.indexOf('*');
}

return tmp;
}

private static boolean isBlanks(String org) {

if (org.indexOf("||") != -1) {
return true;
}
return false;
}

private static int findLastIndex(String org, int fromIndex, char c) {

boolean finding = false;
int last = -1;
int from = fromIndex;

int tmp = org.indexOf(c, from);

if (tmp == from) {

last = tmp;
finding = true;

while (finding) {

from += 1;
tmp = org.indexOf(c, from);
if (tmp == from) {

last = tmp;
continue;
}
else {

finding = false;
break;
}
}
}
if (last == -1) {
last = fromIndex - 1;
}

return last;
}
}

